<template>
  <el-dialog
    title="数据导入"
    :visible.sync="visible"
    width="30%"
    :close-on-click-modal="false"
  >
    <el-upload
      class="upload-demo"
      action="https://jsonplaceholder.typicode.com/posts/"
      :before-upload="beforeUpload"
      :on-success="success"
      :limit="1"
      :file-list="fileList"
      ref="upload"
    >
      标题：
      <el-button size="middle" type="primary" class="el-icon-upload upload"
        >点击上传</el-button
      >
      <div slot="tip" class="el-upload__tip">
        支持扩展名：xls、xlsx，文件不得大于1M
      </div>
    </el-upload>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
    <!-- <input type="file" @change="setfile"> -->
  </el-dialog>
</template>

<script>
import { goodsDataInsert } from '@/api/gdInfo'
export default {
  name: 'Dkd3InsertDataDialog',

  data () {
    return {
      visible: false,
      fileList: [],
      file: null
    }
  },
  watch: {
    visible (val) {
      if (!val) {
        this.fileList = []
        this.file = null
      }
    }
  },
  mounted () {},

  methods: {
    // async setfile (e) {
    //   const formData = new window.FormData()
    //   formData.append('file', e.target.files[0])
    //   // console.log(e.target.files[0])
    //   console.log(formData.get('file'))
    //   const res = await goodsDataInsert(formData)
    //   console.log(res)
    // },
    beforeUpload (file) {
      // console.log(file)
      const types = [
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'application/vnd.ms-excel'
      ]
      if (!types.includes(file.type)) {
        this.$message.error('上传图片只能是 xls、xlsx 格式!')
        return false
      }
      //  检查大小
      const maxSize = 1 * 1024 * 1024
      if (maxSize < file.size) {
        this.$message.error('图片大小最大不能超过1M')
        return false
      }
      return true
    },
    success (response, file, fileList) {
      const formData = new FormData()
      formData.append('fileName', file.raw)
      // goodsDataInsert(formData)
      this.fileList = fileList
      this.file = formData
    },
    async submit () {
      // goodsDataInsert
      await goodsDataInsert(this.file)
      this.visible = false
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-dialog__footer {
  text-align: center;
}
/deep/.upload {
  padding: 12px 80px;
  .el-icon-upload {
    font-size: 20px;
  }
}
/deep/.el-dialog {
  border-radius: 20px;
}
</style>
